﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSTreeThesis.Utilities
{
   public class SAXTransform
    {
        public char[] SaxTranform(float[] data, double max, double min,int length, int dimensions)
        {
            char[] sax = new char[dimensions];
            char[] symbols = new char[length];
            float interval = 0;


            for (int i = 0; i < length; i++)
            {
                symbols[i] = Convert.ToChar(i + 33);
            }


            interval = (float)Math.Round(Math.Abs(max - min) / length, 2);


            for (int i = 0; i < dimensions; i++)
            {
                if (data[i] < min)
                {
                    sax[i] = symbols[0];

                }
                else if (data[i] > max)
                {
                    sax[i] = symbols[length - 1];

                }
                else
                {
                    bool flag = false;
                    for (int j = 0; j < symbols.Length; j++)
                    {
                        if (data[i] >= min + interval * j)
                        {
                            if (data[i] < min + interval * (j + 1))
                            {
                                sax[i] = symbols[j];
                                flag = true;
                            }
                            else if (data[i] >= min + interval * symbols.Length)
                            {
                                sax[i] = symbols[length - 1];
                                flag = true;
                            }
                        }

                        if (flag == true) break;
                    }


                }

            }
            return sax;
        }

        public char[] SaxTranform(double[] data, double max, double min, int length, int dimensions)
        {
            char[] sax = new char[dimensions];
            char[] symbols = new char[length];
            float interval = 0;
   

            for (int i = 0; i < length; i++)
            {
                symbols[i] = Convert.ToChar(i + 33);
            }


            interval = (float)Math.Round(Math.Abs(max - min) / length, 2);
     

            for (int i = 0; i < dimensions; i++)
            {
                if (data[i] < min)
                {
                    sax[i] = symbols[0];
                   
                }
                else if (data[i] > max)
                {
                    sax[i] = symbols[length - 1];
                   
                }
                else
                {
                    bool flag = false;
                    for (int j = 0; j < symbols.Length; j++)
                    {
                        if (data[i] >= min + interval * j)
                        {
                            if (data[i] < min + interval * (j + 1))
                            {
                                sax[i] = symbols[j];
                                flag = true;
                            }
                            else if (data[i] >= min + interval * symbols.Length)
                            {
                                sax[i] = symbols[length - 1];
                                flag = true;
                            }
                        }

                        if (flag == true) break;
                    }

                 
                }

                
            }
            return sax;
        }
    }
}
